package com.study.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.constant.RedisKeyConstant;
import com.study.convention.errorCode.BaseErrorCode;
import com.study.convention.exception.ServiceException;
import com.study.dao.entity.TLink;
import com.study.dao.mapper.TLinkMapper;
import com.study.dto.req.RecycleBinPageReqDTO;
import com.study.dto.req.RecycleBinReqDTO;
import com.study.dto.resp.ShortLinkPageRespDTO;
import com.study.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author canace
 * @version 1.0
 * @description 短链接回收站服务实现类
 * @date 2024/2/4 11:28
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<TLinkMapper, TLink> implements RecycleBinService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public void saveRecycleBin(RecycleBinReqDTO requestParam) {
        // 即将改短连接的enableStatus改为1
        LambdaUpdateWrapper<TLink> set = Wrappers.lambdaUpdate(TLink.class)
                .eq(TLink::getGid, requestParam.getGid())
                .eq(TLink::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(TLink::getEnableStatus, 0)
                .eq(TLink::getDelFlag, 0)
                .set(TLink::getEnableStatus, 1);
        int update = baseMapper.update(null, set);
        if (update == 0) {
            throw new ServiceException(BaseErrorCode.RECYCLE_BIN_UPDATE_ERROR);
        }
        // 删除缓存
        stringRedisTemplate.delete(String.format(RedisKeyConstant.GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public IPage<ShortLinkPageRespDTO> getRecycleBinPage(RecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<TLink> tLinkLambdaQueryWrapper = Wrappers.lambdaQuery(TLink.class)
                .in(TLink::getGid, requestParam.getGidList())
                .eq(TLink::getDelFlag, 0)
                .eq(TLink::getEnableStatus, 1)
                .orderByDesc(TLink::getUpdateTime);
        IPage<TLink> page = baseMapper.selectPage(requestParam, tLinkLambdaQueryWrapper);
        if (ObjectUtil.isEmpty(page)) {
            throw new ServiceException(BaseErrorCode.RECYCLE_BIN_PAGE_ERROR);
        }
        return page.convert(tLink -> BeanUtil.toBean(tLink, ShortLinkPageRespDTO.class));
    }

    @Override
    public void recoverRecycleBin(RecycleBinReqDTO requestParam) {
        // 即将改短连接的enableStatus由1改为0
        LambdaUpdateWrapper<TLink> set = Wrappers.lambdaUpdate(TLink.class)
                .eq(TLink::getGid, requestParam.getGid())
                .eq(TLink::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(TLink::getEnableStatus, 1)
                .eq(TLink::getDelFlag, 0)
                .set(TLink::getEnableStatus, 0);
        int update = baseMapper.update(null, set);
        if (update == 0) {
            throw new ServiceException(BaseErrorCode.RECYCLE_BIN_UPDATE_ERROR);
        }
        // 删除缓存
        stringRedisTemplate.delete(String.format(RedisKeyConstant.GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public void removeRecycleBin(RecycleBinReqDTO requestParam) {
        // 即将改短连接的delFlag改为1
        LambdaUpdateWrapper<TLink> set = Wrappers.lambdaUpdate(TLink.class)
                .eq(TLink::getGid, requestParam.getGid())
                .eq(TLink::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(TLink::getEnableStatus, 1)
                .eq(TLink::getDelFlag, 0)
                .set(TLink::getDelFlag, 1);
        int update = baseMapper.update(null, set);
        if (update == 0) {
            throw new ServiceException(BaseErrorCode.RECYCLE_BIN_UPDATE_ERROR);
        }
        // 删除缓存
        stringRedisTemplate.delete(String.format(RedisKeyConstant.GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
    }
}
